/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2022-09-30 14:29:50
 * @LastEditors: sueRimn
 * @LastEditTime: 2023-02-24 10:38:39
 */
let util = require("../util");
let { query } = require("../db/pool")
exports.add = (req, res) => {
  let params = req.body;
  params.id = util.uuidGenerator();
  let strSql = "insert into page(id,code,name,description,sort_number,create_author,last_modified_author,data_table,page_component,page_data_view,page_request) values('" + params.id + "' ,'" + params.code + "' ,'" + params.name + "' ,'" + params.description + "' ,'" + params.sort_number + "' ,'" + params.create_author + "' ,'" + params.create_author + "' ,'" + params.data_table + "' ,'" + params.page_component + "' ,'" + params.page_data_view + "' ,'" + params.page_request + "')";
  pageAdd(strSql, res);
}
exports.getList = (req, res) => {
  let params = req.body;
  let wh = 'where ';
  for (let key in params) {
    (!util.isEmpty(params[key])) ? wh += key + "='" + params[key] + "' and " : "";
  }
  wh = wh.substring(0, wh.lastIndexOf("and")) + ";";
  let strSql = "select * from page ";
  wh.trim() != "where" ? strSql += wh : strSql += ";";
  getPageList(strSql, res);
}
exports.edit = (req, res) => {
  let params = req.body;
  let strSql = "update page set code='" + params.code + "' ,description='" + params.description + "' ,sort_number='" + params.sort_number + "' ,last_modified_author='" + params.last_modified_author + "' ,name='" + params.name + "' ,data_table='" + params.data_table + "' ,page_component='" + params.page_component + "' ,page_data_view='" + params.page_data_view + "' ,page_request='" + params.page_request + "' where id='" + params.id + "';";
  pageEdit(strSql, res)
}
exports.delete = (req, res) => {
  let params = req.query;
  let strSql = "delete from page where id='" + params.id + "';";
  pageDelete(strSql, res);
}


async function getPageList(sql, res) {
  let rtn = await query(sql);
  if (rtn.status) {
    let dt = rtn.data;
    for (let i = 0; i < dt.length; i++) {
      let str = "";
      dt[i].page_component_arr = [];
      dt[i].page_data_view_arr = [];
      dt[i].page_request_arr = [];
      let cpArr = dt[i].page_component ? dt[i].page_component.split(",") : [];
      for (let k = 0; k < cpArr.length; k++) {
        str = 'select * from component where id = "' + cpArr[k] + '";';
        let rtn = await query(str);
        dt[i].page_component_arr.push(rtn.data[0]);
      }
      let dvArr = dt[i].page_data_view ? dt[i].page_data_view.split(",") : [];
      for (let k = 0; k < dvArr.length; k++) {
        str = 'select * from data_view where id = "' + dvArr[k] + '";';
        let rtn = await query(str);
        dt[i].page_data_view_arr.push(rtn.data[0]);
      }
      let rqArr = dt[i].page_request ? dt[i].page_request.split(",") : [];
      for (let k = 0; k < rqArr.length; k++) {
        str = 'select * from request_config where id = "' + rqArr[k] + '";';
        let rtn = await query(str);
        dt[i].page_request_arr.push(rtn.data[0]);
      }
    }
    rtn.data = dt;
    res.send(rtn);
  } else {
    res.send(rtn);
  }
}
async function pageEdit(sql, res) {
  let rtn = await query(sql);
  res.send(rtn);
}
async function pageDelete(sql, res) {
  let rtn = await query(sql);
  res.send(rtn);
}
async function pageAdd(sql, res) {
  let rtn = await query(sql);
  res.send(rtn);
}
